13041
5138
Ho un array come nome statistico
Mela
X
Banana
Y
Kiwi
z
Devo mettere mela, banana e kiwi in un array Fruits e X, Y e Z in un array chiamato alfabeti.
Qualche semplice meccanismo C # per questo, per favore? 
Utilizzare l'overload IEnumerable  .Where che fornisce l'indice.
var fruits = statsname.Where ((s, i) => i% 2 == 0) .ToArray ();
var alfabeti = statsname.Where ((s, i) => i% 2! = 0) .ToArray ();
|
Rubato da Come ottenere elementi alternativi utilizzando Enumerable in C #
var fruit = myArray.Where ((t, i) => i% 2 == 0) .ToArray ();
var alfabeti = myArray.Where ((t, i) => i% 2 == 1) .ToArray ();
|
Se ho capito bene, chiedi cosa vuoi è molto semplice:
Vuoi mettere i frutti in un array di frutti e lo stesso per gli alfabeti e vengono visualizzati alternativamente in array statsname quindi:
for (int i = 0, j = 0; i  list = new List  () {"apple", "X", "banana", "Y", "Kiwi", "z"};
var result = list.Select ((l, i) => new {l, i})
.GroupBy (p => p.i% 2)
.Seleziona (x => x.Select (v => v.l) .ToList ())
.Elencare();
Quindi hai un elenco di elenchi:
|
list  fruits = new List  ();
list  alfabeto = nuovo List  ();
for (int i = 0; i  everyOther  (IEnumerable  raccolta)
{
using (var e = collection.GetEnumerator ()) {
while (e.MoveNext ()) {
rendimento rendimento e.Current;
e.MoveNext (); // salta uno
}
}
}
Puoi usare System.Linq.Skip per saltare il primo elemento.
string [] words = "mela X banana Y Kiwi z" .Split ();
var fruit = everyOther (parole);
var alfabeti = everyOther (words.Skip (1));
Basta usarli come una nuova raccolta o chiamare .ToArray () o .ToList () su di essi:
foreach (stringa f nei frutti)
Console.WriteLine (f);
string [] anArray = fruits.ToArray (); // using System.Linq
Ora hai quello che ti serve.
Gli iteratori sono metodi che restituiscono il rendimento, vedere iteratori (Guida per programmatori C #). Questa è una caratteristica molto forte della lingua. Puoi:
saltare gli elementi
decorare gli elementi
modificare l'ordine
sequenze concatenate (vedere System.Linq.Concat)
...
|
Ecco un po 'di codice funzionante, speriamo che questo ti sia utile:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
spazio dei nomi ReadFile
{
programma di classe
{
static void ReadFile (string filePath, List  custumerNames, List  phoneNumbers)
{
string line = string.Empty;
var fileStream = nuovo StreamReader (filePath);
bool isPhoneNumber = true;
while ((line = fileStream.ReadLine ())! = null)
{
if (isPhoneNumber)
{
phoneNumbers.Add (Convert.ToInt32 (line));
isPhoneNumber = false;
}
altro
{
custumerNames.Add (riga);
isPhoneNumber = true;
}
}
fileStream.Close ();
}
static void Main (string [] args)
{
Console.WriteLine ("Ho iniziato a leggere il file ...");
List  custumersNamesList = new List  ();
List  custumersPhonesNumbers = new List  ();
ReadFile ("SampleFile.txt", custumersNamesList, custumersPhonesNumbers);
// Supponendo che entrambi gli elenchi abbiano la stessa lunghezza.
for (int i = 0; i